package com.teen9g.infoplatformserver.controller;

import com.teen9g.dto.PostsAddDTO;
import com.teen9g.entity.Posts;
import com.teen9g.infoplatformserver.service.PostsService;
import com.teen9g.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/posts")
public class PostsController {
    @Autowired
    private PostsService postsService;

    /**
     * 根据id查询帖子内容
     *
     * @param id
     * @return
     */
    @GetMapping("/content/{id}")
    public Result<String> getPostsContentById(@PathVariable Integer id) {
        log.info("根据id查询帖子内容,查询的id为:{}", id);
        String contents = postsService.getPostsContentById(id);
        return Result.success(contents);
    }

    /**
     * 查询所有帖子
     *
     * @return
     */
    @GetMapping
    public Result<List<Posts>> getAllPosts() {
        log.info("查询所有帖子");
        List<Posts> posts = postsService.getAllPosts();
        return Result.success(posts);
    }

    /**
     * 根据id查询帖子
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<Posts> getPosts(@PathVariable Integer id) {
        Posts contents = postsService.getPostsById(id);
        log.info("根据id查询帖子,查询的id为:{}", id);
        return Result.success(contents);
    }

    /**
     * 添加帖子
     *
     * @param postsAddDTO
     * @return
     */
    @PostMapping("/add")
    public Result<String> addPosts(@RequestBody PostsAddDTO postsAddDTO) {
        postsService.addPosts(postsAddDTO);
        log.info("添加帖子,帖子内容为:{}", postsAddDTO);
        return Result.success();
    }

    /**
     * 根据内容查询帖子
     *
     * @param likecontent
     * @return
     */
    @GetMapping("/{likecontent}")
    public Result<String> getPostsLike(@PathVariable Integer likecontent) {
        return Result.success();
    }
}
